package com.me.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.me.entity.Admin;
import com.me.service.AdminService;
import com.me.util.EasyUIFormat;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
	/**
	 * 注入业务组件
	 */
	@Resource
	private AdminService adminService;

	/**
	 * 跳转到admin主页
	 */
	@RequestMapping("/index.do")
	public String toAdmin(){
		return "admin/main/admin";
	}
	
	/**
	 * 查询所有admin
	 */
	@RequestMapping("/queryPage.do")
	@ResponseBody
	public Map<String, Object> queryAll(){
		List<Admin> admins = adminService.queryAll();
		return EasyUIFormat.formatEntities(admins);
	}
	
	/**
	 * 新增管理员
	 */
	@RequestMapping("/insert.do")
	@ResponseBody
	public Map<String, Object> insert(Admin admin){
		try {
			LOG.info("--------插入记录：" + admin + "--------");
			adminService.insert(admin);
			return messageSuccess();
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "插入记录失败！");
		}
	}
	
	/**
	 * 修改账户
	 */
	@RequestMapping("/update.do")
	@ResponseBody
	public Map<String, Object> update(Admin admin){
		try {
			LOG.info("--------修改记录：" + admin + "--------");
			adminService.update(admin);
			return messageSuccess();
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "修改记录失败！");
		}
	}
	
	/**
	 * 删除账户
	 */
	@RequestMapping("/delete.do")
	@ResponseBody
	public Map<String, Object> delete(Admin admin){
		try {
			LOG.info("--------删除记录：" + admin + "--------");
			if("admin".equals(admin.getAccount())){
				return messageFailure("admin账户不允许删除！", "删除记录失败！");
			}
			adminService.delete(admin);
			return messageSuccess();
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "删除记录失败！");
		}
	}
}
